const path = require('path')
// 阿里云OSS开发文档如下：
// https://help.aliyun.com/document_detail/111256.html?spm=5176.8466032.help.dexternal.7d9e1450J1pEk1#concept-d5g-1y1-dhb
const OSS = require('ali-oss')
const { REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET, BUCKET } = require('../config')

const client = new OSS({
  region: REGION,
  accessKeyId: ACCESS_KEY_ID,
  accessKeySecret: ACCESS_KEY_SECRET,
  bucket: BUCKET
})

// 上传文件
const put = async (fileName, filePath) => {
  try {
    const result = await client.put(fileName, path.normalize(filePath))
    return result
  } catch(err) {
    console.log('上传oss出错: ', err)
    return err
  }
}

module.exports = { put }